package org.example.lingvanex_translation;

import com.google.gson.Gson;
import okhttp3.*;
import org.example.translation.*;

import java.util.Map;

public class LingvanexTranslation extends MapLangSupportHandler {

    private final OkHttpClient client = new OkHttpClient();
    private final MediaType mediaType = MediaType.parse("application/json");
    private final Gson gson = new Gson();

    public LingvanexTranslation(PropValueSupplier propValueSupplier) {
        super(propValueSupplier);
        translationApiType = TranslationApiType.Continuous;
    }

    @Override
    protected void initSupportComparisonTable(Map<String, String> countryComparisonTable) {
        countryComparisonTable.put("en", "en_US");
        countryComparisonTable.put("cs", "cs_CZ");
        countryComparisonTable.put("de", "de_DE");
        countryComparisonTable.put("el", "el_GR");
        countryComparisonTable.put("fi", "fi_FI");
        countryComparisonTable.put("fr", "fr_CA");
        countryComparisonTable.put("hr", "hr_HR");
        countryComparisonTable.put("in", "id_ID");
        countryComparisonTable.put("it", "it_IT");
        countryComparisonTable.put("ja", "ja_JP");
        countryComparisonTable.put("ko", "ko_KR");
        countryComparisonTable.put("nl", "nl_NL");
        countryComparisonTable.put("pt", "pt_PT");
        countryComparisonTable.put("ro-rRO", "ro_RO");
        countryComparisonTable.put("ru", "ru_RU");
        countryComparisonTable.put("sl", "sl_SI");
        countryComparisonTable.put("sr", "sr-Cyrl_RS");
        countryComparisonTable.put("zh", "zh-Hans_CN");
        countryComparisonTable.put("zh-rHK", "zh-Hant_TW");
        countryComparisonTable.put("zh-rMO", "zh-Hant_TW");
        countryComparisonTable.put("zh-rTW", "zh-Hant_TW");
        //添加的部分
        countryComparisonTable.put("es", "es_ES");
    }

    @Override
    public TranslationResponseBean requestTranslation(TranslationPostBean postBean) {
        PostBean requestBean = new PostBean()
                .setData(postBean.getSourceString())
                .setTo(getSupportLang(postBean.getTargetCountry()));
        RequestBody body = RequestBody.create(mediaType, gson.toJson(requestBean));
        Request request = new Request.Builder()
                .url("https://api-b2b.backenster.com/b1/api/v3/translate")
                .post(body)
                .addHeader("accept", "application/json")
                .addHeader("content-type", "application/json")
                .addHeader("Authorization", propValueSupplier.get("lingvanex.token"))
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            RespBean respBean = gson.fromJson(string, RespBean.class);
            if (respBean != null && respBean.getErr() == null) {
                return new TranslationResponseBean()
                        .setTargetResult(respBean.getResult())
                        .setSourceString(respBean.getFrom())
                        .setSourceCountry(postBean.getSourceCountry())
                        .setTargetCountry(postBean.getTargetCountry());
            } else {
                RuntimeException runtimeException = new RuntimeException("server response:" + string +
                        "\nSourceCountry:" + postBean.getSourceCountry()+
                        "\nTargetCountry:" + postBean.getTargetCountry() +
                        "\nContent:" + postBean.getSourceString());
                runtimeException.printStackTrace();
                translation(postBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new TranslationResponseBean()
                .setTargetResult("未完成翻译")
                .setSourceString(postBean.getSourceString())
                .setSourceCountry(postBean.getSourceCountry())
                .setTargetCountry(postBean.getTargetCountry());
    }
}
